﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-26 19:30:43
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	/// <summary>
	///应用认证信息 数据访问层
	/// </summary>	
	internal partial class PassportTicket:IPassportTicket
	{		
    
         //连接字符串
         public virtual string ConnectionStringKey
         {
             get
             {
                 return ConstantUtility.ConnectionEjautobaseKey;
             }
         }
           
         ///<summary>
         /// 获取最大主键ID，针对非自增列主键 
         /// 暂时无用
         ///</summary>
         public int GetMaxID()
         {
             int nReturn=0;
             string szSql=@"SELECT ISNULL(MAX(AppSignInID),0)+1
                            FROM [PassportTicket]";             
             Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
             DbCommand dbc = db.GetSqlStringCommand(szSql);
             try
             {
                 object o = db.ExecuteScalar(dbc);
                 if (o!=null)
                 {
                     nReturn = Convert.ToInt32(o);
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return nReturn;               
         }
                
     
         ///<summary>
         /// datareader转对象
         ///</summary>
         private PassportTicketInfo GetByReader(IDataReader dr)
         {
            PassportTicketInfo info = new PassportTicketInfo();
            object obj;
            obj=dr["AppSignInID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.AppSignInID = obj.ToString();
            }
            obj=dr["SignInID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.SignInID = obj.ToString();
            }
            obj=dr["AppID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.AppID = obj.ToString();
            }
            obj=dr["AppSignInTime"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.AppSignInTime = Convert.ToDateTime(obj);
            }
            obj=dr["AppSignInTimeOut"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.AppSignInTimeOut = Convert.ToDateTime(obj);
            }
            obj=dr["AppSignInUrl"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.AppSignInUrl = obj.ToString();
            }
            obj=dr["AppSignInIP"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.AppSignInIP = obj.ToString();
            }
            obj=dr["AppLogOffUrl"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.AppLogOffUrl = obj.ToString();
            }
            return info;
         }
         
        ///<summary>
        /// 插入接口
        ///</summary>
		public string Insert(PassportTicketInfo info)
		{
            string strSql=@"INSERT INTO [PassportTicket]			
			(			
			[AppSignInID],
			[SignInID],
			[AppID],
			[AppSignInTime],
			[AppSignInTimeOut],
			[AppSignInUrl],
			[AppSignInIP],
			[AppLogOffUrl]
			)
			 VALUES
			(			
			@AppSignInID,
			@SignInID,
			@AppID,
			@AppSignInTime,
			@AppSignInTimeOut,
			@AppSignInUrl,
			@AppSignInIP,
			@AppLogOffUrl
			)";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@AppSignInID",DbType.String, info.AppSignInID);
            db.AddInParameter(dbc, "@SignInID",DbType.String, info.SignInID);
            db.AddInParameter(dbc, "@AppID",DbType.String, info.AppID);
            db.AddInParameter(dbc, "@AppSignInTime",DbType.DateTime, info.AppSignInTime.ToNullDbDateTime());
            db.AddInParameter(dbc, "@AppSignInTimeOut",DbType.DateTime, info.AppSignInTimeOut.ToNullDbDateTime());
            db.AddInParameter(dbc, "@AppSignInUrl",DbType.String, info.AppSignInUrl);
            db.AddInParameter(dbc, "@AppSignInIP",DbType.String, info.AppSignInIP);
            db.AddInParameter(dbc, "@AppLogOffUrl",DbType.String, info.AppLogOffUrl);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (string)db.GetParameterValue(dbc,"AppSignInID");
         
		}
		
		///<summary>
        /// 修改接口
        ///</summary>
        public void Update(PassportTicketInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE [PassportTicket] SET ");
            sbSql.AppendLine("[SignInID] = @SignInID,");
                
            sbSql.AppendLine("[AppID] = @AppID,");
                
            sbSql.AppendLine("[AppSignInTime] = @AppSignInTime,");
                
            sbSql.AppendLine("[AppSignInTimeOut] = @AppSignInTimeOut,");
                
            sbSql.AppendLine("[AppSignInUrl] = @AppSignInUrl,");
                
            sbSql.AppendLine("[AppSignInIP] = @AppSignInIP,");
                
            sbSql.AppendLine("[AppLogOffUrl] = @AppLogOffUrl");
                
            sbSql.Append(" where  [AppSignInID] = @AppSignInID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@AppSignInID",DbType.String, info.AppSignInID);
            db.AddInParameter(dbc, "@SignInID",DbType.String, info.SignInID);
            db.AddInParameter(dbc, "@AppID",DbType.String, info.AppID);
            db.AddInParameter(dbc, "@AppSignInTime",DbType.DateTime, info.AppSignInTime.ToNullDbDateTime());
            db.AddInParameter(dbc, "@AppSignInTimeOut",DbType.DateTime, info.AppSignInTimeOut.ToNullDbDateTime());
            db.AddInParameter(dbc, "@AppSignInUrl",DbType.String, info.AppSignInUrl);
            db.AddInParameter(dbc, "@AppSignInIP",DbType.String, info.AppSignInIP);
            db.AddInParameter(dbc, "@AppLogOffUrl",DbType.String, info.AppLogOffUrl);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        ///<summary>
        /// 删除接口
        ///</summary>
        public void Delete(string appSignInID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [PassportTicket]  ");
            strSql.Append("WHERE [AppSignInID]=@AppSignInID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@AppSignInID",DbType.String, appSignInID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
        
         ///<summary>
         /// 搜索接口
         ///</summary>
         public IList<PassportTicketInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
         {
            IList<PassportTicketInfo> list = new List<PassportTicketInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "[PassportTicket]");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    PassportTicketInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
         }
        
      
        ///<summary>
        ///根据主键取出信息
        ///</summary>
        public PassportTicketInfo GetByID(string appSignInID)
		{
            Model.PassportTicketInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [PassportTicket] ");
            strSql.Append("WHERE [AppSignInID]=@AppSignInID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@AppSignInID",DbType.String, appSignInID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}	
	}
}